﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LiveProjectManager.DDD;


namespace LiveProjectManager.Controllers
{
    public class PostCommentController : Controller
    {
        private static DDD.Services.PostCommentService _service = new DDD.Services.PostCommentService();
        private static DDD.Services.UserService _userService = new DDD.Services.UserService();
        private static DDD.Services.PostService _postService = new DDD.Services.PostService();


        // 
        // GET: /PostComment/Index
        public ActionResult Index(int paginateBy = 0, int page = 1, long myPostId = 0)
        {
            if (myPostId != 0)
            {
                DDD.Domains.Post myPost = _postService.getById(myPostId);
                if (myPost == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
                return Json(
                    DDD.Services.PostCommentService.toDict(_service.getAllByPost(myPost, paginateBy, page)),
                    JsonRequestBehavior.AllowGet
                );
            }
            return Json(
                DDD.Services.PostCommentService.toDict(_service.getAll(paginateBy, page)),
                JsonRequestBehavior.AllowGet
            );
        }


        // 
        // GET: /PostComment/{Id}
        public ActionResult Details(int id)
        {
            DDD.Domains.PostComment myPost = _service.getById(id);

            // Can't find a matching post.
            if (myPost == null)
                return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

            // Return formatted resource.
            return Json(myPost, JsonRequestBehavior.AllowGet);
        }


        //
        // POST: /PostComment/Create
        [HttpPost]
        public ActionResult Create(FormCollection myFormData)
        {
            try
            {
                DDD.Domains.User myUser = _userService.getById(long.Parse(myFormData["UserId"]));
                DDD.Domains.Post myPost = _postService.getById(long.Parse(myFormData["PostId"]));

                // Validate user.
                if (myUser == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "No User found with UserId: " + myFormData["UserId"] + ".");

                // Validate post.
                if (myPost == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "No Post found with PostId: " + myFormData["PostId"] + ".");

                // Try to create new Post.
                if (!_service.create(myFormData["Content"], myUser, myPost))
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Invalid Content parameter.");
                return new HttpStatusCodeResult((int)HttpStatusCode.Created);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Invalid UserId or PostId parameter (must be longs).");
            }
        }


        //
        // DELETE: /PostComment/Delete
        [HttpDelete]
        public ActionResult Delete(FormCollection myFormCollection)
        {
            try
            {
                // Entity to delete is not found.
                if (!_service.exists(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

                // Unknown error occured, unable to delete.
                if (!_service.remove(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);

                // Deletion successful
                return new HttpStatusCodeResult((int)HttpStatusCode.NoContent);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Invalid Id parameter (must be a long).");
            }
        }
    }
}